package com.levi.juc.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 生产者，消费者问题    线程间通信问题
 */
public class ProducerAndConsumer {
    public static void main(String[] args) {
//        Data data = new Data();
//        for (int i = 0; i < 8; i++) {
//            new Thread(data::increment,"A").start();
//            new Thread(data::decrement,"B").start();
//            new Thread(data::increment,"C").start();
//            new Thread(data::decrement,"D").start();
//        }

        Data1 data1 = new Data1();
        for (int i = 0; i < 8; i++) {
            new Thread(data1::printA, "A").start();
            new Thread(data1::printB, "B").start();
            new Thread(data1::printC, "C").start();
        }
    }
}


/**
 * 数据实体类
 */
class Data {
    private int num = 0;
    //创建锁
    Lock lock = new ReentrantLock();

    //创建condition对象
    Condition condition = lock.newCondition();

    //+1操作
    public void increment() {
        //获得锁
        lock.lock();
        try {
            //业务逻辑
            while (num != 0) {
                condition.await();
            }
            num++;
            System.out.println(Thread.currentThread().getName() + "num= " + num);
            //执行完之后唤醒其他线程
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    //-1操作
    public void decrement() {
        lock.lock();
        try {
            while (num == 0) {
                condition.await();
            }
            num--;
            System.out.println(Thread.currentThread().getName() + "num= " + num);
            //执行完之后唤醒其他线程
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();


        }
    }
}


/**
 * 用condition实现精准唤醒线程
 */
class Data1 {
    private int num = 1;
    private final Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();

    public void printA() {
        lock.lock();
        try {
            while (num != 1) {
                condition1.await();
            }
            num = 2;
            System.out.println(Thread.currentThread().getName() + ">>>AAA");
            //精准唤醒线程2
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public void printB() {
        lock.lock();
        try {
            while (num != 2) {
                condition2.await();
            }
            num = 3;
            System.out.println(Thread.currentThread().getName() + ">>>BBB");
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printC() {
        lock.lock();
        try {
            while (num != 3) {
                condition3.await();
            }
            num = 1;
            System.out.println(Thread.currentThread().getName() + ">>>CCC");
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
